package com.firer.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements Adapter.ViewHolder.ClickListener{

    private Adapter adapter;
    private ActionMode actionMode;
    private ActionModeCallback actionModeCallback = new ActionModeCallback();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        adapter = new Adapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(this, OrientationHelper.HORIZONTAL, false));
        //recyclerView.setLayoutManager(new GridLayoutManager(this, 4, OrientationHelper.HORIZONTAL, false));
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));
    }

    @Override
    public void onItemClicked(int pos) {
        if(actionMode != null) {
            toggleSelection(pos);
        } else {
            adapter.removeItem(pos);
        }
    }

    @Override
    public boolean onItemLongClicked(int pos) {
        if(actionMode == null)
            actionMode = startSupportActionMode(actionModeCallback);
        toggleSelection(pos);
        return true;
    }

    private void toggleSelection(int pos) {
        adapter.toggleSelection(pos);
        int cnt = adapter.getSelectedItemCount();
        if(cnt == 0) {
            actionMode.finish();
        } else {
            actionMode.setTitle(cnt + "");
            actionMode.invalidate();
        }
    }

    private class ActionModeCallback implements ActionMode.Callback {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.selected_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//            if( item.getItemId() == R.id.delete ) {
//                Log.d("dhy", "menu_del");
//                adapter.removeItems(adapter.getSelectedItems());
//                mode.finish();
//                Log.d("dhy", "81");
//                return true;
//            }
            switch(item.getItemId()) {
                case R.id.delete:
                    Log.d("dhy","86");
                    adapter.removeItems(adapter.getSelectedItems());
                    mode.finish();
                    Log.d("dhy","89");
                    return false;
                default:
                    return true;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            adapter.clearSelection();
            actionMode = null;
        }
    }
}
